package com.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.ShippingMapper;
import com.example.model.dto.Result;
import com.example.model.dto.ShippingListDTO;
import com.example.model.entity.Shipping;
import com.example.model.vo.OrderShippingVO;
import com.example.model.vo.UserVO;
import com.example.service.IShippingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: backend
 * @author: zk
 * @create: 2023-04-23 16:06
 **/
@Service
@Slf4j
public class ShippingServiceImpl extends ServiceImpl<ShippingMapper, Shipping> implements IShippingService {

    @Resource
    private HttpSession session;
    @Resource
    private ShippingMapper shippingMapper;

    @Override
    public Result addShipping(OrderShippingVO orderShippingVO) {
        // 1.创建地址对象
        Shipping shipping = BeanUtil.copyProperties(orderShippingVO, Shipping.class);

        // 2.获取用户信息
        UserVO user = (UserVO) session.getAttribute("user");
        if (user == null) {
            return Result.fail("用户未登录,无法获取当前用户信息");
        }

        // 3.获取用户id
        String userId = user.getId();

        // 4.设置参数
        shipping.setUserId(userId);

        // 5.保存
        boolean save = save(shipping);

        // 6.保存失败
        if (!save) {
            return Result.fail("新建地址失败");
        }

        // 7.成功
        return Result.ok("新建地址成功");

    }

    @Override
    public Result deleteShipping(String shippingId) {
        if (Integer.parseInt(shippingId) <= 0) {
            return Result.fail("输入参数有误");
        }

        int delete = shippingMapper.deleteById(shippingId);
        if (delete == 0) {
            return Result.fail("删除地址失败");
        }

        return Result.ok("删除地址成功");
    }

    @Override
    public Result updateShipping(String id, OrderShippingVO orderShippingVO) {

        if (Integer.parseInt(id) <= 0) {
            return Result.fail("输入参数有误");
        }

        // 设置更新条件
        UpdateWrapper<Shipping> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);

        // 查询数据库中的地址信息
        Shipping s = query().eq("id", id).one();

        // 设置更新的参数
        Shipping shipping = BeanUtil.copyProperties(orderShippingVO, Shipping.class);
        shipping.setId(s.getId());
        shipping.setUserId(s.getUserId());

        // 更新
        int update = shippingMapper.update(shipping, updateWrapper);

        // 更新失败
        if (update == 0) {
            return Result.fail("更新地址失败");
        }

        // 成功
        return Result.ok("更新地址成功");
    }

    @Override
    public Result getShipping() {

        // 获取登录用户信息
        UserVO user = (UserVO) session.getAttribute("user");

        // 判断用户是否登录
        if (user == null) {
            return Result.fail("请登录之后查询");
        }

        // 获取用户id
        String userId = user.getId();

        // 查询用户对应的收货地址
        List<Shipping> shippingList = query().eq("userId", userId).list();

        ShippingListDTO shippingListDTO = new ShippingListDTO();
        shippingListDTO.setList(shippingList);

        // 返回
        return Result.ok(shippingListDTO);
    }
}
